package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.BdpRealTaskAlarmSendLog;
import cn.getech.data.development.mapper.BdpRealTaskAlarmSendLogMapper;
import cn.getech.data.development.model.dto.RealTaskAlarmInfoDto;
import cn.getech.data.development.service.IBdpRealTaskAlarmSendLogService;
import cn.getech.data.development.service.IBdpRealTaskAlarmService;
import cn.getech.data.intelligence.common.utils.Constant;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 实时任务告警发送告警的记录表 服务实现类
 * </p>
 *
 * @author hujz
 * @since 2021-04-08
 */
@Service
public class BdpRealTaskAlarmSendLogServiceImpl extends ServiceImpl<BdpRealTaskAlarmSendLogMapper, BdpRealTaskAlarmSendLog> implements IBdpRealTaskAlarmSendLogService {

    @Autowired
    private IBdpRealTaskAlarmService iBdpRealTaskAlarmService;

    @Override
    public void deleteLogs(Integer alarmId) {
        baseMapper.deleteLogs(alarmId);
    }

    @Override
    public void deleteLogs(Integer alarmId, List<Integer> deleteTaskMenuIds) {
        baseMapper.deleteAndTaskLogs(alarmId,deleteTaskMenuIds);
    }

    @Override
    public void cronCheackSendLogs() {
        //查询到没有没有发送的所有的消息信息
        List<RealTaskAlarmInfoDto> lists = baseMapper.logsNeedPushLists();
        if(CollectionUtil.isNotEmpty(lists)){
            //挑选出来是已经禁用的状态。如果禁用，则直接删除
            List<RealTaskAlarmInfoDto> needDeletes = new ArrayList<>();
            List<RealTaskAlarmInfoDto> others = new ArrayList<>();
            lists.forEach(o -> {
                if(Objects.equals(o.getState(), Constant.StateEnum.ENABLE.getValue())){
                    if(null != o.getNoDisturbingEntTime() && null != o.getNoDisturbingStartTime()  && StringUtils.isNotEmpty(o.getEmail())){
                        others.add(o);
                    }else{
                        needDeletes.add(o);
                    }
                }else{
                    needDeletes.add(o);
                }
            });
            //需要删除的数据
            if(CollectionUtil.isNotEmpty(needDeletes)){
                List<Integer> needDeleteIds = needDeletes.stream().map(RealTaskAlarmInfoDto::getSendLogId).distinct().collect(Collectors.toList());
                baseMapper.updateByIds(needDeleteIds);
            }
            //需要验证时间是否达到推送的时间了
            if(CollectionUtil.isNotEmpty(others)){
                List<RealTaskAlarmInfoDto> needChangeStatusLists = iBdpRealTaskAlarmService.cheackAndPush(others, LocalTime.now());
                if(CollectionUtil.isNotEmpty(needChangeStatusLists)){
                    List<Integer> ids = needChangeStatusLists.stream().map(RealTaskAlarmInfoDto::getSendLogId).distinct().collect(Collectors.toList());
                    baseMapper.updateIsSendByIds(ids);
                }
            }
        }
    }

    @Override
    public void deleteLogsByTriggerCondition(Integer alarmId, Integer triggerCondition) {
        baseMapper.deleteLogsByTriggerCondition(alarmId,triggerCondition);
    }
}
